package com.train.common.model.page;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.train.common.utils.object.BeanUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageSorted {

    @Getter
    public enum SortedType {
        ASC(0, "asc"), DESC(1, "desc");

        private final Integer code;

        private final String name;

        SortedType(Integer code, String name) {
            this.code = code;
            this.name = name;
        }
    }

    private String field;

    private Integer sortedType;

    /**
     * 排序构造
     * @param field
     * @param sortedType
     * @param <T>
     * @param <R>
     */
    public <T, R> PageSorted(SFunction<T, R> field, SortedType sortedType) {
        this.field = BeanUtils.getFieldName(field);
        this.sortedType = sortedType.getCode();
    }

    /**
     * 默认倒序
     * @param field
     * @param <T>
     * @param <R>
     */
    public <T, R> PageSorted(SFunction<T, R> field) {
        this(field, SortedType.DESC);
    }
}